package com.education.platform.api.controller.goods;

import com.alibaba.fastjson.JSON;
import com.education.platform.api.service.GoodsService;

import com.education.platform.api.vo.request.GoodsQueryVO;
import com.education.platform.api.vo.response.*;
import com.education.platform.common.util.BeanUtil;
import com.education.platform.dao.entity.GoodsDO;
import com.education.platform.dao.model.GoodsModel;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
* describe: 商城
* @author : tomato
* @date: 2020-06-14 09:47:11.640
**/
@RestController
@Api("商城")
@RequestMapping("goods")
@Validated
public class GoodsController {
	
    private static final Logger LOGGER = LoggerFactory.getLogger(GoodsController.class);
    


    @Autowired
    private GoodsService goodsService;

    /**
     * 分页查询
     * @param queryVO 分页查询参数
     * @return 分页参数
     */
    @ApiOperation(value = "分页查询")
    @PostMapping("getPageList")
    public GoodsPageInfoVO selectPageList(@RequestBody @Valid GoodsQueryVO queryVO) {
        LOGGER.info("getPageList:"+ JSON.toJSONString(queryVO));
        GoodsModel goodsDTO = BeanUtil.convert(queryVO, GoodsModel.class);
        goodsDTO.setDeleted(false);
        goodsDTO.setPublish(true);
        Page<UserOrderResponseVO> page = PageHelper.startPage(queryVO.getPageNum(), queryVO.getPageSize());
        List<GoodsDO> goodsDTOS = goodsService.getSortList(goodsDTO);
        GoodsPageInfoVO pageInfoVO = new GoodsPageInfoVO();
        BeanUtils.copyProperties(page.toPageInfo(), pageInfoVO);
        List<GoodsResponseVO> voList = BeanUtil.convert(goodsDTOS, GoodsResponseVO.class);
        pageInfoVO.setList(voList);
        return pageInfoVO;
    }




}
